//
//  homeStatusCell.swift
//  sinazd
//
//  Created by apple on 16/3/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class homeStatusCell: UITableViewCell {

    
    var bottomViewTopConstrant:Constraint?
    
  //设置一个属性，调用set方法的时候，进行cell里面控件的赋值
    var status:Status? {
        
        didSet{
            
            originalView.originalStatus = status
            
            
            bottomViewTopConstrant?.uninstall()
            
            //如果是转发微博的话 就给转发微博数据模型赋值
            if let re = status?.retweeted_status {
                retweetedView.retweetedStatus = re
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstrant = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }else{
                retweetedView.retweetedStatus = nil
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstrant = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }

            
        }
        
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加子视图
    func setupUI() {
     
       
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        
        contentView.addSubview(retweetedView)
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(originalView)
        }

       
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
//            make.height.equalTo(100)
        }
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
           // make.top.equalTo(originalView.snp_bottom)
            
           self.bottomViewTopConstrant = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint

            make.height.equalTo(36)
        }
        //让cell自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }

       
    }
    
    //懒加载 一个originalStatusView 
    private lazy var originalView:originalStatusView = originalStatusView()
    private lazy var bottomView:statusBottomView = statusBottomView()
    //转发视图
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()


}
